package com.example.Android_test.activity.training;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.specs.WordSpec;
import com.example.Android_test.util.DownStepSecondTimer;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 18.05.13
 * Time: 10:33
 * To change this template use File | Settings | File Templates.
 */
public class Training_Sprint extends Abstract_Training {
    public static final int ONE_SPRINT_TIME = 10;

    private boolean isOnAir;

    private TextView ts_textViewTimer;
    private TextView ts_textViewEng;
    private TextView ts_textViewRus;

    private Button ts_buttonYes;
    private Button ts_buttonNo;

    private WordSpec trainingWord;
    private boolean isWordCombinationTrue;

    private int goodCount;
    private int badCount;

    @Override
    public void onStop() {
        super.onStop();
        isOnAir = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        isOnAir = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        isOnAir = true;
    }


    @Override
    protected void onFirstRun() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected String getKeyTrainingTimeSpent() {
        return SettingsHelper.KEY_TRAINING_TIME_SPENT_SPRINT;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.training_sprint);
        setTitle("Training_Sprint");

        isOnAir = true;

        ts_textViewEng = (TextView) findViewById(R.id.ts_engTextView);
        ts_textViewRus = (TextView) findViewById(R.id.ts_rusTextView);
        ts_textViewTimer = (TextView) findViewById(R.id.ts_timerTextView);

        ts_buttonYes = (Button) findViewById(R.id.ts_button_yes);
        ts_buttonNo = (Button) findViewById(R.id.ts_button_no);

        ts_buttonYes.setOnClickListener(new YesButtonListener());
        ts_buttonNo.setOnClickListener(new NoButtonListener());

        DownStepSecondTimer timer = new DownStepSecondTimer();
        timer.addStepListener(new DownStepSecondTimerOnStepListener());
        timer.addFinishListener(new DownStepSecondTimerOnFinishListener());
        timer.startWithInitialSeconds(ONE_SPRINT_TIME);

        // TODO enable alert
        //alertOnOpen();
        restartSprint();
    }

    private void restartSprint() {
        goodCount = 0;
        badCount = 0;

        next();
    }

    private void alertOnOpen() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("Are you sure?");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new AlertDialogButtonOKListener());
       // alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
    }

    private void alertOnFinish() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Reset...");
        alertDialog.setMessage("Are you sure?");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new AlertDialogButtonOKListener());
        // alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
    }

    @Override
    protected void next() {
        try {
            trainingWord = SOWEngine.INSTANCE.getWordToTraining();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return;
        }

        String engWord = trainingWord.getWord();
        String rusWord = trainingWord.getTranslationVariantSpecs().get(0).getTranslate();

        Random random = new Random();
        if (random.nextInt(1) == 0) {
            try {
                WordSpec randomWord = SOWEngine.INSTANCE.getOneRandomWord(trainingWord);
                rusWord = randomWord.getTranslationVariantSpecs().get(0).getTranslate();
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                return;
            }
            isWordCombinationTrue = false;
        } else {
            isWordCombinationTrue = true;
        }

        ts_textViewEng.setText(engWord);
        ts_textViewRus.setText(rusWord);
    }

    private class AlertDialogButtonOKListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            restartSprint();
        }
    }

    private class DownStepSecondTimerOnStepListener implements DownStepSecondTimer.ActionListener {
        @Override
        public void actionPerformed(Object o) {
            ts_textViewTimer.setText(o.toString());
        }
    }

    private class DownStepSecondTimerOnFinishListener implements DownStepSecondTimer.ActionListener {
        @Override
        public void actionPerformed(Object o) {
            if (isOnAir) alertOnFinish();
        }
    }

    private class YesButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (isWordCombinationTrue) {
               SOWEngine.INSTANCE.changeWordRating(trainingWord, 1);
                goodCount++;
            } else {
                SOWEngine.INSTANCE.changeWordRating(trainingWord, -1);
                badCount++;
            }

            next();
        }
    }

    private class NoButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (isWordCombinationTrue) {
                SOWEngine.INSTANCE.changeWordRating(trainingWord, -1);
                badCount++;
            } else {
                SOWEngine.INSTANCE.changeWordRating(trainingWord, 1);
                goodCount++;
            }

            next();
        }
    }
}